package com.yhp.web;

import com.yhp.bean.Role;
import com.yhp.bean.Users;
import com.yhp.service.impl.RoleServletImpl;
import com.yhp.service.impl.UsersServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/user/users")
public class UserServlet extends HttpServlet {
    public UsersServiceImpl usersService = new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("get".equals(method)){
            //查看全部信息
            getUsers(req,resp);
        }else if("delete".equals(method)){
            //删除用户信息
            deleteUser(req,resp);
        }else if("update".equals(method)){
            //修改用户信息
            upDateUser(req,resp);
        }else if("add".equals(method)){
            //添加用户信息
            addUser(req,resp);
        }else if("roleAdd".equals(method)){
            //给角色列表赋值 add页面
            roleAdd(req,resp);
        }else if("roleEdit".equals(method)){
            roleEdit(req,resp);
        }else if("roleInfo".equals(method)){
            roleInfo(req,resp);
        }
    }

    //进入add页面   获取启用的角色信息到角色选择
    protected void roleAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleServletImpl roleServlet = new RoleServletImpl();
        List<Role> role = roleServlet.getRoleGo();
        req.setAttribute("a",role);
        req.getRequestDispatcher("add.jsp").forward(req,resp);

    }
    //进去edit页面   获取启用的角色信息到角色选择
    protected void roleEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        Users id = usersService.getId(Integer.parseInt(userId));
        req.setAttribute("id",id);
        RoleServletImpl roleServlet = new RoleServletImpl();
        List<Role> role = roleServlet.getRoleGo();
        req.setAttribute("e",role);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);

    }

    //进去info页面   获取角色信息
    protected void roleInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        Users id = usersService.getId(Integer.parseInt(userId));
        req.setAttribute("lookid",id);
        RoleServletImpl roleServlet = new RoleServletImpl();
        List<Role> role = roleServlet.getRoleGo();
        req.setAttribute("i",role);
        req.getRequestDispatcher("info.jsp").forward(req,resp);

    }

    //查看全部信息
    protected void getUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageUtil pageUtil = new PageUtil();
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageindex值的话那就默认为第一页
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        List<Users> users = usersService.GetUsers(index, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(index);
        pageUtil.setDataList(users);
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp );


    }

    //删除用户信息
    protected void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("userId");

        int delete = usersService.delete(Integer.parseInt(id));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(delete>0){
            writer.println("<script>alert('删除成功');location.href='/power/user/users?method=get';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power/user/users?method=get';</script>");

        }
    }

    //修改用户信息
    protected void upDateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取信息
        String id = req.getParameter("id");
        String loginName = req.getParameter("loginName");
        String passWord = req.getParameter("passWord");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String addRess = req.getParameter("addRess");
        String cardid = req.getParameter("cardid");
        String indesc = req.getParameter("desc");

        //把信息存入对象类
        Users users = new Users();
        users.setUserId(Integer.parseInt(id));
        users.setLoginName(loginName);
        users.setPassWord(passWord);
        users.setRealName(realName);
        users.setSex(Integer.parseInt(sex));
        users.setRoleId(Integer.parseInt(roleId));
        users.setEmail(email);
        users.setPhone(phone);
        users.setAddRess(addRess);
        users.setCardid(cardid);
        users.setIndesc(indesc);

        int i = usersService.upDate(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/power/user/users?method=get';</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/power/user/users?method=roleEdit&userId="+id+"';</script>");

        }


    }

    //添加用户信息
    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取信息
        String loginName = req.getParameter("loginName");
        String passWord = req.getParameter("passWord");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String addRess = req.getParameter("addRess");
        String cardid = req.getParameter("cardid");
        String indesc = req.getParameter("desc");

        //把信息存入对象类
        Users users = new Users();
        users.setLoginName(loginName);
        users.setPassWord(passWord);
        users.setRealName(realName);
        users.setSex(Integer.parseInt(sex));
        users.setRoleId(Integer.parseInt(roleId));
        users.setEmail(email);
        users.setPhone(phone);
        users.setAddRess(addRess);
        users.setCardid(cardid);
        users.setIndesc(indesc);

        //调用selvet方法
        int i = usersService.addUser(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            System.out.println(i);
            writer.println("<script>alert('添加成功');location.href='/power/user/users?method=get'</script>");
        }else{
            writer.println("<script>alert('添加失败');location.href='/power/user/users?method=get'</script>");
        }


    }






}
